﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Transaction.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the Transaction type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;

    /// <summary>
    /// Defines the <seealso cref="Transaction"/> type.
    /// </summary>
    public class Transaction : ModelBase, IDataErrorInfo
    {
        /// <summary>
        /// Backing field for IsTransferTransaction
        /// </summary>
        private bool _isTransferTransaction;

        /// <summary>
        /// Backing field for Transaction ID
        /// </summary>
        private int _transactionId;

        /// <summary>
        /// Backing field for title
        /// </summary>
        private string _title;

        /// <summary>
        /// Backing f0ield for Amount
        /// </summary>
        private decimal _amount;

        /// <summary>
        /// Backing field for Account
        /// </summary>
        private int _account;

        /// <summary>
        /// Backing field forTransaction Type
        /// </summary>
        private TransactionTypes _transactionType;

        /// <summary>
        /// Backing field for details
        /// </summary>
        private string _details;

        /// <summary>
        /// Backing field for date
        /// </summary>
        private DateTime _date;

        /// <summary>
        /// Member variable _tags
        /// </summary>
        private IEnumerable<Tag> _tags;

        /// <summary>
        /// Backing field for linked transaction date
        /// </summary>
        private DateTime _linkedTransactionDate;

        /// <summary>
        /// Backing field for linked transaction account
        /// </summary>
        private int _linkedTransactionAccount;

        /// <summary>
        /// Backing field for related transaction id
        /// </summary>
        private int _relatedTransactionId;

        /// <summary>
        /// Gets or sets the transaction id.
        /// </summary>
        /// <value>
        /// The transaction id.
        /// </value>
        public int TransactionId
        {
            get
            {
                return _transactionId;
            }

            set
            {
                if (_transactionId != value)
                {
                    RaisePropertyChangingEvent("TransactionId");
                    _transactionId = value;
                    RaisePropertyChangedEvent("TransactionId");
                }
            }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                if (_title != value)
                {
                    RaisePropertyChangingEvent("Title");
                    _title = value;
                    RaisePropertyChangedEvent("Title");
                }
            }
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        /// <value>
        /// The amount.
        /// </value>
        public decimal Amount
        {
            get
            {
                return _amount;
            }

            set
            {
                if (_amount != value)
                {
                    RaisePropertyChangingEvent("Amount");
                    _amount = value;
                    RaisePropertyChangedEvent("Amount");
                }
            }
        }

        /// <summary>
        /// Gets or sets the account.
        /// </summary>
        /// <value>
        /// The account.
        /// </value>
        public int Account
        {
            get
            {
                return _account;
            }

            set
            {
                if (_account != value)
                {
                    RaisePropertyChangingEvent("Account");
                    _account = value;
                    RaisePropertyChangedEvent("Account");
                }
            }
        }

        /// <summary>
        /// Gets or sets the details.
        /// </summary>
        /// <value>
        /// The details.
        /// </value>
        public string Details
        {
            get
            {
                return _details;
            }

            set
            {
                RaisePropertyChangingEvent("Details");
                _details = value;
                RaisePropertyChangedEvent("Details");
            }
        }

        /// <summary>
        /// Gets or sets the type of the transaction.
        /// </summary>
        /// <value>
        /// The type of the transaction.
        /// </value>
        public TransactionTypes TransactionType
        {
            get
            {
                return _transactionType;
            }

            set
            {
                RaisePropertyChangingEvent("TransactionType");
                _transactionType = value;
                RaisePropertyChangedEvent("TransactionType");
                IsTransferTransaction = value == TransactionTypes.Transfer;
            }
        }

        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        /// <value>The date of transaction</value>
        public DateTime Date
        {
            get
            {
                return _date;
            }

            set
            {
                if (_date != value)
                {
                    RaisePropertyChangingEvent("Date");
                    _date = value;
                    RaisePropertyChangedEvent("Date");
                }
            }
        }

        /// <summary>
        /// Gets or sets the tags.
        /// </summary>
        /// <value>The comma separated list of tags. </value>
        public IEnumerable<Tag> Tags
        {
            get
            {
                return _tags;
            }

            set
            {
                if (_tags != value)
                {
                    RaisePropertyChangingEvent("Tags");
                    _tags = value;
                    RaisePropertyChangedEvent("Tags");
                    RaisePropertyChangedEvent("TagsValue");
                }
            }
        }

        /// <summary>
        /// Gets the tags value.
        /// </summary>
        /// <value>The tags value.</value>
        public string TagsValue
        {
            get
            {
                var sb = new StringBuilder();
                foreach (var tag in Tags)
                {
                    sb.AppendFormat("{0}, ", tag.TagId);
                }

                return sb.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the account.
        /// </summary>
        /// <value>
        /// The account.
        /// </value>
        public int LinkedTransactionAccount
        {
            get
            {
                return _linkedTransactionAccount;
            }

            set
            {
                if (_linkedTransactionAccount != value)
                {
                    RaisePropertyChangingEvent("LinkedTransactionAccount");
                    _linkedTransactionAccount = value;
                    RaisePropertyChangedEvent("LinkedTransactionAccount");
                }
            }
        }

        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        /// <value>The date of transaction</value>
        public DateTime LinkedTransactionDate
        {
            get
            {
                return _linkedTransactionDate;
            }

            set
            {
                if (_linkedTransactionDate != value)
                {
                    RaisePropertyChangingEvent("LinkedTransactionDate");
                    _linkedTransactionDate = value;
                    RaisePropertyChangedEvent("LinkedTransactionDate");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [new account].
        /// </summary>
        /// <value>
        /// True if new account; otherwise, false.
        /// </value>
        public bool IsTransferTransaction
        {
            get
            {
                return _isTransferTransaction;
            }

            set
            {
                if (_isTransferTransaction != value)
                {
                    RaisePropertyChangingEvent("IsTransferTransaction");
                    _isTransferTransaction = value;
                    RaisePropertyChangedEvent("IsTransferTransaction");
                }
            }
        }

        /// <summary>
        /// Gets or sets the related transaction id.
        /// </summary>
        /// <value>
        /// The related transaction id.
        /// </value>
        public int RelatedTransactionId
        {
            get
            {
                return _relatedTransactionId;
            }

            set
            {
                if (_relatedTransactionId != value)
                {
                    RaisePropertyChangingEvent("RelatedTransactionId");
                    _relatedTransactionId = value;
                    RaisePropertyChangedEvent("RelatedTransactionId");
                }
            }
        }

        #region Implementation of IDataErrorInfo

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <returns>
        /// An error message indicating what is wrong with this object. The default is an empty string ("").
        /// </returns>
        public string Error
        {
            get
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <returns>
        /// The error message for the property. The default is an empty string ("").
        /// </returns>
        /// <param name="columnName">The name of the property whose error message to get. </param>
        public string this[string columnName]
        {
            get
            {
                string result = null;

                if (columnName == "Title")
                {
                    if (string.IsNullOrWhiteSpace(Title))
                    {
                        result = "Title is required";
                    }
                }
                else if (columnName == "Amount")
                {
                    if (Amount == 0M)
                    {
                        result = "Amount is required";
                    }
                }
                else if (columnName == "Date")
                {
                    // TODO: Add proper validation
                    // if (Date < new DateTime(01, 01, 2000))
                    // {
                    //    result = "Valid date is required";
                    // }
                }

                return result;
            }
        }

        #endregion Implementation of IDataErrorInfo
    }
}